home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / pyshared / checkbox / lib / safe.py < prev    next >
Encoding:
Python Source  |  2009-04-27  |  2.6 KB  |  97 lines

  1. #
  2. # This file is part of Checkbox.
  3. #
  4. # Copyright 2008 Canonical Ltd.
  5. #
  6. # Checkbox is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # Checkbox is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Checkbox.  If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. import os
  20. import posixpath
  21. import shutil
  22.  
  23. from stat import ST_MODE, S_IMODE, S_ISFIFO
  24.  
  25.  
  26. def safe_change_mode(path, mode):
  27.     if not posixpath.exists(path):
  28.         raise Exception, "Path does not exist: %s" % path
  29.  
  30.     old_mode = os.stat(path)[ST_MODE]
  31.     if mode != S_IMODE(old_mode):
  32.         os.chmod(path, mode)
  33.  
  34. def safe_make_directory(path, mode=0755):
  35.     if posixpath.exists(path):
  36.         if not posixpath.isdir(path):
  37.             raise Exception, "Path is not a directory: %s" % path
  38.     else:
  39.         os.makedirs(path, mode)
  40.  
  41. def safe_make_fifo(path, mode=0666):
  42.     if posixpath.exists(path):
  43.         mode = os.stat(path)[ST_MODE]
  44.         if not S_ISFIFO(mode):
  45.             raise Exception, "Path is not a FIFO: %s" % path
  46.     else:
  47.         os.mkfifo(path, mode)
  48.  
  49. def safe_remove_directory(path):
  50.     if posixpath.exists(path):
  51.         if not posixpath.isdir(path):
  52.             raise Exception, "Path is not a directory: %s" % path
  53.  
  54.         shutil.rmtree(path)
  55.  
  56. def safe_remove_file(path):
  57.     if posixpath.exists(path):
  58.         if not posixpath.isfile(path):
  59.             raise Exception, "Path is not a file: %s" % path
  60.  
  61.         os.remove(path)
  62.  
  63. def safe_rename(old, new):
  64.     if old != new:
  65.         if not posixpath.exists(old):
  66.             raise Exception, "Old path does not exist: %s" % old
  67.         if posixpath.exists(new):
  68.             raise Exception, "New path exists already: %s" % new
  69.  
  70.         os.rename(old, new)
  71.  
  72. def safe_md5sum():
  73.     try:
  74.         import hashlib
  75.         digest = hashlib.md5()
  76.     except ImportError:
  77.         # for Python << 2.5
  78.         import md5
  79.         digest = md5.new()
  80.  
  81.     return digest
  82.  
  83. def safe_md5sum_file(name):
  84.     md5sum = None
  85.     if posixpath.exists(name):
  86.         file = open(name)
  87.         digest = safe_md5sum()
  88.         while 1:
  89.             buf = file.read(4096)
  90.             if buf == "":
  91.                 break
  92.             digest.update(buf)
  93.         file.close()
  94.         md5sum = digest.hexdigest()
  95.  
  96.     return md5sum
  97.